package com.hq.talk.view.ui;

import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.RelativeLayout;
import android.widget.TabHost;
import android.widget.TextView;

import com.hq.lib_comview.common.ClickCounter;
import com.hq.lib_comview.common.code.Constants;
import com.hq.lib_comview.common.code.IntentKey;
import com.hq.lib_comview.common.code.ReturnCode;
import com.hq.lib_comview.common.utils.DialogUtils;
import com.hq.lib_comview.common.utils.LogUtils;
import com.hq.lib_comview.common.utils.ToastUtil;
import com.hq.lib_comview.view.tabhost.CustomFragmentTabHost;
import com.hq.talk.R;
import com.hq.talk.common.BaseEvent;
import com.hq.talk.common.Session;
import com.hq.talk.service.NetBroadCastReciver;
import com.hq.talk.util.ClearCacheUtil;
import com.hq.talk.view.base.BaseCommAty;
import com.hq.talk.view.frg.ExpoFrg;
import com.hq.talk.view.frg.MineFrg;
import com.hq.talk.view.frg.MyCardsFrg;
import com.hq.talk.view.frg.NewsFrg;
import com.hq.talk.view.frg.WorkFrg;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import butterknife.BindView;

public class MainAty extends BaseCommAty implements NetBroadCastReciver.NetEvent, TabHost.OnTabChangeListener {

    @BindView(R.id.id_main_content_ll)
    FrameLayout mMainContentLL;
    //底部导航栏类
    @BindView(android.R.id.tabhost)
    CustomFragmentTabHost mTabHost;

    public static final String[] mTabStrs = new String[]{"消息", "博览会", "工作台", "名片夹", "个人"};

    public static NetBroadCastReciver.NetEvent mNetEvent;

    private ClickCounter mCounter;

    @Override
    protected boolean isSupportBack() {
        return false;
    }

    @Override
    protected boolean isSupportSwipeback() {
        return false;
    }

    /**
     * 判断网络状态
     *
     * @param netMobile
     */
    @Override
    public void onNetChange(int netMobile) {

    }

    @Nullable
    @Override
    public View loadView(LayoutInflater inflater, View childView) {
        return super.loadView(inflater, inflater.inflate(R.layout.aty_main, null, false));
    }

    @Override
    public void initPresenter() {
    }

    @Override
    public void initView() {
        LogUtils.LogD("进来了", "----MainAty----");
        mNetEvent = MainAty.this;
        mCounter = new ClickCounter(2, 2000);
        mCounter.setListener(new ClickCounter.OnCountListener() {
            @Override
            public void onCount(int currentTime) {
                ToastUtil.showToastById(aty, R.string.exit_2_app);
            }

            @Override
            public void onFinish() {
                finish();
            }
        });

        initBottomTab();
        RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) mMainContentLL.getLayoutParams();
        //xml中是35多一個px
        layoutParams.setMargins(0, 0, 0, (int) (0 - getResources().getDimension(R.dimen.px36)));
    }

    @Override
    public void initData() {
        EventBus.getDefault().register(this);

    }

    private void initBottomTab() {
        mTabHost = (CustomFragmentTabHost) findViewById(android.R.id.tabhost);

        mTabHost.setup(this, getSupportFragmentManager(), R.id.id_main_content_ll);

        mTabHost.setOnTabChangedListener(this);

        mTabHost.getTabWidget().setDividerDrawable(null);
        mTabHost.addTab(mTabHost.newTabSpec(mTabStrs[0]).setIndicator(getLayoutInflater().inflate(R.layout.frg_main_news, null)),
                NewsFrg.class, null);
        mTabHost.addTab(mTabHost.newTabSpec(mTabStrs[1]).setIndicator(getLayoutInflater().inflate(R.layout.frg_main_expo, null)),
                ExpoFrg.class, null);
        mTabHost.addTab(mTabHost.newTabSpec(mTabStrs[2]).setIndicator(getLayoutInflater().inflate(R.layout.frg_main_work, null)),
                WorkFrg.class, null);
        mTabHost.addTab(mTabHost.newTabSpec(mTabStrs[3]).setIndicator(getLayoutInflater().inflate(R.layout.frg_main_card, null)),
                MyCardsFrg.class, null);
        mTabHost.addTab(mTabHost.newTabSpec(mTabStrs[4]).setIndicator(getLayoutInflater().inflate(R.layout.frg_main_mine, null)),
                MineFrg.class, null);
    }

    @Override
    public void onTabChanged(String tabId) {
        if (mTabStrs[0].equals(tabId)) {
            initNewsFrgTitleBar(tabId);
        } else if (mTabStrs[1].equals(tabId)) {
            initExpoFrgTitleBar(tabId);
        } else if (mTabStrs[2].equals(tabId)) {
            initWorkFrgTitleBar(tabId);
        } else if (mTabStrs[3].equals(tabId)) {
            initCardFrgTitleBar(tabId);
        } else if (mTabStrs[4].equals(tabId)) {
            initMineFrgTitleBar(tabId);
        }
    }

    /**
     * 处理消息页
     *
     * @param tabId
     */
    private void initNewsFrgTitleBar(String tabId) {
        setTitleText(getString(R.string.hom_tab_news), null);
    }

    /**
     * 处理博览会
     *
     * @param tabId
     */
    private void initExpoFrgTitleBar(String tabId) {
        setTitleText(getString(R.string.hom_tab_expo), null);
    }

    /**
     * 处理工作台
     *
     * @param tabId
     */
    private void initWorkFrgTitleBar(String tabId) {
        setTitleText(getString(R.string.hom_tab_work), null);
    }


    /**
     * 处理名片
     *
     * @param tabId
     */
    private void initCardFrgTitleBar(String tabId) {
        setTitleText(getString(R.string.hom_tab_cards), null);
    }

    /**
     * 处理个人
     *
     * @param tabId
     */
    private void initMineFrgTitleBar(String tabId) {
        setTitleText(getString(R.string.hom_mine), null);
    }


    @Override
    public void onBackPressed() {
        if (null != mCounter) {
            mCounter.countClick();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    //记录当前底部下标的位置
    private int currentTab = 0;

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        outState.putInt(IntentKey.General.KEY_POS, mTabHost.getCurrentTab());
        super.onSaveInstanceState(outState);
    }

    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);
        currentTab = savedInstanceState.getInt(IntentKey.General.KEY_POS);
    }

    private Dialog dialog;

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        int type = intent.getIntExtra(IntentKey.General.KEY_TYPE, Constants.DEFAULTVALUE);
        if (type == ReturnCode.USER_EXPIRE) {
            //用户过期
            if (!Session.getUserIsLogin()) {
                return;
            }

            if (null != dialog && dialog.isShowing()) {
                dialog.dismiss();
            }

            View view = getLayoutInflater().inflate(R.layout.dialog_login_remind, null, false);
            ((TextView) view.findViewById(R.id.id_remind_login_txt)).setText(getString(R.string.login_expire));
            TextView mTxtCancel = (TextView) view.findViewById(R.id.id_login_cancel_txt);
            mTxtCancel.setText(getString(R.string.cancel));
            mTxtCancel.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    dialog.dismiss();
                }
            });
            TextView mTxtConfirm = (TextView) view.findViewById(R.id.id_login_confirm_txt);
            mTxtConfirm.setText(getString(R.string.login_with_space));
            mTxtConfirm.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    dialog.dismiss();
                    showActivity(aty, LoginAty.class);
                }
            });

            if (null == dialog) {
                dialog = DialogUtils.selfDialog(aty, view, true);
            }

            if (!isFinishing() && null != dialog) {
                dialog.show();
            }

            ClearCacheUtil.clearCache(aty, 1, Session.getUserId());

            BaseEvent event = new BaseEvent();
            event.data = IntentKey.Login.KEY_APP_EXIT;
            EventBus.getDefault().post(event);
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void eventUpdate(BaseEvent event) {
        String data = event.data;
        if (TextUtils.equals(data, IntentKey.Login.KEY_APP_EXIT)) {
            //退出登录

        }
    }
}
